package com.ruoyi.car.business.web.controller;

import java.util.List;

import com.ruoyi.car.business.domain.BusAppointment;
import com.ruoyi.car.business.domain.BusStatement;
import com.ruoyi.car.business.service.IBusAppointmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 养修信息预约Controller
 * 
 * @author ruoyi
 * @date 2022-01-26
 */
@Controller
@RequestMapping("/business/appointment")
public class BusAppointmentController extends BaseController
{
    private String prefix = "business/appointment/";

    @Autowired
    private IBusAppointmentService busAppointmentService;

    @RequiresPermissions("business:appointment:view")
    @GetMapping("/listPage")
    public String appointment()
    {
        return prefix + "/appointment";
    }

    /**
     * 查询养修信息预约列表
     */
    @RequiresPermissions("business:appointment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusAppointment busAppointment)
    {
        startPage();
        List<BusAppointment> list = busAppointmentService.selectBusAppointmentList(busAppointment);
        return getDataTable(list);
    }

    /**
     * 导出养修信息预约列表
     */
    @RequiresPermissions("business:appointment:export")
    @Log(title = "养修信息预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusAppointment busAppointment)
    {
        List<BusAppointment> list = busAppointmentService.selectBusAppointmentList(busAppointment);
        ExcelUtil<BusAppointment> util = new ExcelUtil<BusAppointment>(BusAppointment.class);
        return util.exportExcel(list, "养修信息预约数据");
    }

    /**
     * 新增养修信息预约
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存养修信息预约
     */
    @RequiresPermissions("business:appointment:add")
    @Log(title = "养修信息预约", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusAppointment busAppointment)
    {
        return toAjax(busAppointmentService.insertBusAppointment(busAppointment));
    }

    /**
     * 修改养修信息预约
     */
    @RequiresPermissions("business:appointment:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BusAppointment busAppointment = busAppointmentService.selectBusAppointmentById(id);
        mmap.put("busAppointment", busAppointment);
        return prefix + "/edit";
    }

    /**
     * 修改保存养修信息预约
     */
    @RequiresPermissions("business:appointment:edit")
    @Log(title = "养修信息预约", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusAppointment busAppointment)
    {
        return toAjax(busAppointmentService.updateBusAppointment(busAppointment));
    }

    /**
     * 删除养修信息预约
     */
    @RequiresPermissions("business:appointment:remove")
    @Log(title = "养修信息预约", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busAppointmentService.deleteBusAppointmentByIds(ids));
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @RequiresPermissions("business:appointment:cancel")
    @RequestMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(Long id){
        // 修改预约记录:用户取消
        busAppointmentService.cancel(id);
        return AjaxResult.success();
    }

    /**
     * 用户到店
     * @param id
     * @return
     */
    @RequiresPermissions("business:appointment:arrival")
    @RequestMapping("/arrival")
    @ResponseBody
    public AjaxResult arrival(Long id){
        // 修改预约记录:用户到店
        busAppointmentService.arrival(id);
        return AjaxResult.success();
    }

    /**
     * 结算单
     * @param appointmentId
     * @return
     */
    @RequestMapping("/generateStatement")
    @ResponseBody
    public AjaxResult generateStatement(Long appointmentId){
        BusStatement statement = busAppointmentService.generateStatement(appointmentId);
        return AjaxResult.success(statement.getId());
    }
}
